package com.deyuanyun.pic.settings.controller.base;

import com.alibaba.fastjson.JSON;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.InspAppraisePushMsg;
import com.deyuanyun.pic.settings.service.msgtemplate.InspResultInputPushMsg;
import com.deyuanyun.pic.settings.service.msgtemplate.RiskSystemPushMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *  客户端阀值设置调用的controller接口
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-06-28
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@RestController
@RequestMapping("/settings/thresholdInteface")
public class BizMsgThresholdIntefaceController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BizMsgThresholdIntefaceController.class);

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    /**
     * 风险评价数据录入过后调用的接口
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-06-28
     */
    @RequestMapping("/riskInput.json")
    public void riskInput(final String codeList, HttpServletResponse response) {
        if(!StringUtils.isEmpty(codeList)) {
            taskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    if(LOGGER.isInfoEnabled()) {
                        LOGGER.info("接口调用执行开始，传入的统一编号为：" + codeList);
                    }
                    try {
                        AbstractPushMsgTemplate riskSystemPushMsg  = new RiskSystemPushMsg(); //风险评价页面-----风险等级相关消息推送模板
                        Map map = new HashMap();
                        map.put(RiskSystemPushMsg.CODE_LIST_KEY, Arrays.asList(codeList.split("#")));
                        riskSystemPushMsg.setInterface(true, map);
                        riskSystemPushMsg.pushMsg();
                        if(LOGGER.isInfoEnabled()) {
                            LOGGER.info("接口调用处理完成");
                        }
                    } catch (Exception e) {
                        LOGGER.error("风险评价数据录入后异常", e);
                    }
                }
            });
            if(LOGGER.isInfoEnabled())
                LOGGER.info("风险评价数据录入调用成功");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0100", "调用成功")));
        } else {
                if(LOGGER.isInfoEnabled())
                    LOGGER.error("风险评价数据录入调用失败，统一编号为空");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0000", "传入的风险评价统一编号为空")));
        }
    }

    /**
     * 内检测数据录入后调用的接口
     * @param taskId 检测任务id
     * @param taskName 任务名称
     * @param featureType 缺陷类型
     */
    @RequestMapping("/inspResultInput.json")
    public void inspResultInput(final String taskId, final String taskName, final String featureType,
                                HttpServletResponse response) {
        if(StringUtils.isEmpty(taskId) || StringUtils.isEmpty(taskName)
                || StringUtils.isEmpty(featureType)) {
            LOGGER.error("请传入必须的任务id、任务名称、缺陷类型");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0000", "请传入必须的任务id、任务名称、缺陷类型")));
        } else {
            taskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    String decodeFeatureType = null;
                    String decodeTaskName = null;
                    try {
                        decodeFeatureType = URLDecoder.decode(featureType, "utf-8");
                        decodeTaskName = URLDecoder.decode(taskName, "utf-8");

                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put(InspResultInputPushMsg.FEATURE_TYPE_KEY, decodeFeatureType);
                        map.put(InspResultInputPushMsg.TASK_ID_KEY, taskId);
                        map.put(InspResultInputPushMsg.TASK_NAME_KEY, decodeTaskName);

                        AbstractPushMsgTemplate inspResultInputPushMsg  = new InspResultInputPushMsg(); //完整性评价页面-----内检测结果录入后推送模板
                        inspResultInputPushMsg.setInterface(true, map);
                        inspResultInputPushMsg.pushMsg();
                    } catch (Exception e) {
                        LOGGER.error("内检测数据录入后调用异常", e);
                    }
                }
            });
            if(LOGGER.isInfoEnabled())
                LOGGER.info("内检测数据录入后调用的接口调用成功");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0100", "调用成功")));
        }
    }

    /**
     * 内检测评价结果录入后调用的消息推送
     * @param taskId 缺陷评价任务id
     * @param defectId 缺陷id，以#分割
     * @param response
     */
    @RequestMapping("/inspAppraiseInput.json")
    public void inspAppraiseCInput(final String taskId, final String defectId, HttpServletResponse response) {
        if(StringUtils.isEmpty(taskId) || StringUtils.isEmpty(defectId)) {
            LOGGER.error("请传入缺陷id、缺陷评价任务id");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0000", "请传入缺陷id、缺陷评价任务id")));
        }else {
            taskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        AbstractPushMsgTemplate inspAppraisePushMsg = new InspAppraisePushMsg();
                        Map<String, Object> paramMap = new HashMap<String, Object>();
                        paramMap.put(InspAppraisePushMsg.TASK_ID_KEY, taskId);
                        paramMap.put(InspAppraisePushMsg.DEFECT_ID_KEY, Arrays.asList(defectId.split("#")));
                        inspAppraisePushMsg.setInterface(true, paramMap);
                        inspAppraisePushMsg.pushMsg();
                    } catch (Exception e) {
                        LOGGER.error("内检测评价结果录入后调用异常", e);
                    }
                }
            });
        }
        if(LOGGER.isInfoEnabled())
            LOGGER.info("内检测评价结果录入后接口调用成功");
            AjaxResponse.sendText(response, JSON.toJSONString(putCodeMsg("0100", "调用成功")));
    }



    //返回接口调用结果
    public Map<String, Object> putCodeMsg(Object code, Object msg, Object result) {
        Map<String, Object> map = new HashMap<String, Object>(3);
        map.put("code", code);
        map.put("msg", msg);
        if(result != null)
            map.put("result", result);
        return map;
    }

    //返回接口调用结果
    public Map<String, Object> putCodeMsg(Object code, Object msg) {
        return putCodeMsg(code, msg, null);
    }

}
